// pages/games/bigPlate/index.js
// var utils = require('./utils.js');
// var Animation = require('./Animation.js');
// var Pointer = require('./Pointer.js');
// var Wheel = require('./Wheel.js');
const {
  GamesModel
} = require('../../../utils/games-model.js');
const gameModel = new GamesModel()
const {
  MemberModel
} = require('../../../utils/member-model.js');
const memberModel = new MemberModel();
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    wheelImg: ['https://oss.kiscloud.net/wechat/images/wheel2.png', 'https://oss.kiscloud.net/wechat/images/wheel3.png', 'https://oss.kiscloud.net/wechat/images/wheel4.png', 'https://oss.kiscloud.net/wechat/images/wheel5.png', 'https://oss.kiscloud.net/wechat/images/wheel6.png'],
    point: 'https://oss.kiscloud.net/wechat/images/game_start.png',
    arr1: {
      img: 'https://oss.kiscloud.net/wechat/images/wheel2.png'
    },
    arr2: {
      img: 'https://oss.kiscloud.net/wechat/images/wheel3.png'
    },
    arr3: {
      img: 'https://oss.kiscloud.net/wechat/images/wheel4.png'
    },
    arr4: {
      img: 'https://oss.kiscloud.net/wechat/images/wheel5.png'
    },
    arr5: {
      img: 'https://oss.kiscloud.net/wechat/images/wheel6.png'
    },
    giftImg: 'https://oss.kiscloud.net/wechat/images/games/gift.png',
    jfImg: 'https://oss.kiscloud.net/wechat/images/games/integral.png',
    authShow: false,
    wheelBg: false,
    isShow2: false,
    isClick: true,
    listArr: [],
    isShow: true,
    themeUrL: 'https://oss.kiscloud.net/wechat/images/share2.jpg',
    disCountImg: 'https://oss.kiscloud.net/wechat/images/discount_coupon.png',
    ratio: 1,
    isShow2: true,
    innerText: '',
    idex: 0,
    time: 3,
    imgUrl: 'https://oss.kiscloud.net/wechat/images/winning_ticket.png',
    isRotate: 0,
    isdisabled: false,
    gameConfigName: '', // 游戏名，
    memberId: '', // 会员 id
    activityType: '', // 游戏类型
    gameConfigId: '', // 活动id
    gift: {}, // 礼品信息
    detailData: {},
    activityId: '', // 活动id
  },
  // 在玩一次
  tryAngain: function () {
    this.setData({
      isShow: true,
      isRotate: 0,
      time: 0
    })
  },
  start: function () {
    if (this.data.isClick) {
      if (app.globalData.isLogin) {
        memberModel.userInfo({
          merchantId: wx.getStorageSync('merchantId')
        }).then(res => {
          this.setData({
            memberId: res.memberId
          })
          this.setData({
            isClick: false
          })
          // wx.showLoading()
          let that = this;
          that.setData({
            time: 3
          })
          const merchantId = wx.getStorageSync('merchantId')
          gameModel.gameLottery({
            "merchantId": merchantId,
            "memberId": that.data.memberId,
            "refActivityId": this.data.detailData.activityId || ''
          }).then(data => {
            let deg = 360 / that.data.listArr.length
            let idex = that.data.listArr.length - 1
            that.data.listArr.forEach((ele, index) => {
              if (ele == data.activityGiftName) {
                idex = index
              }
            })
            let n = -deg / 2 - deg * idex
            that.startRotate(n)
            setTimeout(() => {
              this.setData({
                isShow: false,
                gift: data,
                isClick: true
              })
              console.log(111)
              console.log(that.data.gift)
              // wx.hideLoading()
            }, 3500)
            // that.startRotate(n)
          }).catch(err => {
            // wx.showToast({
            //   title: `${err.error}`,
            //   icon: 'none',
            //   duration: 2000
            // })
            that.setData({
              isShow2: false,
              innerText: `${err.error}`
            })
            that.setData({
              isClick: true
            })
          })
          // _this.getGameLottery(data.memberId)
        }).catch(err => {
          that.setData({
            isShow2: false,
            innerText: `${err.error}`
          })
          // wx.showToast({
          //   title: `${err.error}`,
          //   icon: 'none',
          //   duration: 2000
          // })
        })
      } else {
        this.setData({
          authShow: true
        })
      }
    } else {
      return false
    }
  },
  startNo: function () {
    this.setData({
      isShow2: false,
      innerText: `暂无此类活动`
    })
  },
  // 显示遮罩层
  showModal: function () {
    // 如果已经登录但是没有会员id
    if (app.globalData.isLogin) {
      if (this.data.memberId && this.data.memberId != '') {
        wx.navigateTo({
          url: `/pages/games/winningRecord/index?refActivityId=${this.data.activityType}&memberId=${this.data.memberId}`,
        })
      } else {
        memberModel.userInfo({
          merchantId: wx.getStorageSync('merchantId')
        }).then(data => {
          this.setData({
            memberId: data.memberId
          })
          wx.navigateTo({
            url: `/pages/games/winningRecord/index?refActivityId=${this.data.activityType}&memberId=${this.data.memberId}`,
          })
        })
      }
    } else {
      this.setData({
        authShow: true
      })
    }
  },
  startRotate: function (n) {
    // 指定获奖结果
    let rotdeg = this.data.isRotate //传入指定的旋转角度，内部指定获奖结果。在指定角度上加上旋转基数模拟转盘随机旋转。
    rotdeg = rotdeg + n + 1440 //1440为旋转基数，最低要旋转说1440度，即4圈。rand-(rand%60) 这个是让指针永远停在扇形中心的算法。n + 是为了重复点击的时候有足够的旋角度。
    this.setData({
      isRotate: rotdeg
    })
  },
  ok: function () {
    // console.log(111)
  },
  // 获取游戏详情
  getGangeDetai: function () {
    // if (app.globalData.isLogin) {
    const merchantId = wx.getStorageSync('merchantId')
    // '40ed3fb4-6b8a-4606-9cb3-a1e8bba5584b' ||
    gameModel.gameDetail({
      merchantId: merchantId,
      activityType: this.data.activityType,
      // activityType: 2,
      // activityStatus: 1,
      activityId: this.data.activityId
      // activityName: '大转盘'
    }).then(data => {
      // if(data){
      if (data === null) {
        wx.showToast({
          title: `${'暂无此类型活动'}`,
          icon: 'none',
          duration: 2000
        })
        return null;
      }
      if (data.activityStatus == 0) {
        wx.showToast({
          title: `${'活动未开始'}`,
          icon: 'none',
          duration: 2000
        })
      } else if (data.activityStatus == 2) {
        wx.showToast({
          title: `${'活动已结束'}`,
          icon: 'none',
          duration: 2000
        })
      }
      let list = []
      data.activityGiftList.map(ele => {
        list.push(ele.activityGiftName)
      })
      // data.activityRule = data.activityRule
      // data.activityRule = (data.activityRule || '').split('\n')
      list.push(data.reminder)
      this.setData({
        detailData: data,
        listArr: list,
        wheelBg: true
      })
      // }
      // else{
      //   this.setData({
      //     isShow2: false,
      //     innerText: `${err.error ? err.error : '暂无此类活动'}`
      //   })
      // }   
    }).catch(err => {
      // wx.showToast({
      //   title: `${err.error ? err.error : '活动未开始或者已经结束'}`,
      //   icon: 'none',
      //   duration: 2000
      // })
      this.setData({
        isShow2: false,
        innerText: `${err.error ? err.error : '暂无此类活动'}`
      })
    })
    // }else{

    // }
  },
  // 获取用户信息
  getUserInfo: function () {
    const _this = this;
    memberModel.userInfo({
      merchantId: wx.getStorageSync('merchantId')
    }).then(data => {
      _this.setData({
        memberId: data.memberId
      })
      // _this.getGameLottery(data.memberId)
    }).catch(err => {
      // wx.showToast({
      //   title: `${err.error}`,
      //   icon: 'none',
      //   duration: 2000
      // })
      _this.setData({
        isShow2: false,
        innerText: `${err.error}`
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  // 授权成功的回调
  loginSucceed: function () {
    // this.getGangeDetai()
    // this.setData({
    //   radarImg: null
    // })
    this.getUserInfo()
  },
  onLoad: function (options) {
    console.log('dd', options)
    this.setData({
      activityType: options.activityType,
      gameConfigName: options.gameConfigName,
      activityId: options.id
    })
    if (wx.getStorageSync('merchantId')) {
      this.getGangeDetai()
    } else {
      app.getInfo().then(data => {
        this.getGangeDetai()
      })
    }
    if (app.globalData.isLogin) {
      this.getUserInfo()
    }
    // } else {
    //   this.setData({
    //     authShow: true
    //   })
    // }
    // this.initState()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // this.init()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: this.data.detailData && this.data.detailData.shareTitle,
      // imageUrl: this.data.detailData && this.data.detailData.shareImage,
      imageUrl: this.data.themeUrL,
      path: `pages/games/bigWheel/index?activityType=${this.data.activityType}&gameConfigName=${this.data.gameConfigName}&shareMemberId=${this.data.memberId}&id=${this.data.activityId}&activityId=${this.data.activityId}&isShare=1`
    }
  }
})